import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)


const routes = [{
        path: '/',
        name: 'Main',
        redirect: '/layout'
    }, {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('../views/login/Login.vue')
    },
    {
        path: '/layout',
        name: 'Layout',
        component: () =>
            import ('../views/Layout.vue'),

        children: [{
            path: '',
            name: 'Home',
            component: () =>
                import ('../views/Home.vue')
        }, {
            path: 'user',
            name: 'User',
            component: () =>
                import ('../views/user/User.vue')
        }, {
            path: 'add-user',
            name: 'AddUser',
            component: () =>
                import ('../views/user/AddUser.vue')
        }, {
            path: 'edit-user',
            name: 'EditUser',
            component: () =>
                import ('../views/user/EditUser.vue')
        }, {
            path: 'admin',
            name: 'Admin',
            component: () =>
                import ('../views/admin/Admin.vue')
        }, {
            path: 'add-admin',
            name: 'AddAdmin',
            component: () =>
                import ('../views/admin/AddAdmin.vue')
        }, {
            path: 'edit-admin',
            name: 'EditAdmin',
            component: () =>
                import ('../views/admin/EditAdmin.vue')
        }, {
            path: 'category',
            name: 'Category',
            component: () =>
                import ('../views/category/Category.vue')
        }, {
            path: 'add-category',
            name: 'AddCategory',
            component: () =>
                import ('../views/category/AddCategory.vue')
        }, {
            path: 'edit-category',
            name: 'EditCategory',
            component: () =>
                import ('../views/category/EditCategory.vue')
        }, {
            path: 'book',
            name: 'Book',
            component: () =>
                import ('../views/book/Book.vue')
        }, {
            path: 'add-book',
            name: 'AddBook',
            component: () =>
                import ('../views/book/AddBook.vue')
        }, {
            path: 'edit-book',
            name: 'EditBook',
            component: () =>
                import ('../views/book/EditBook.vue')
        }, {
            path: 'borrow',
            name: 'Borrow',
            component: () =>
                import ('../views/borrow/Borrow.vue')
        }, {
            path: 'add-borrow',
            name: 'AddBorrow',
            component: () =>
                import ('../views/borrow/AddBorrow.vue')
        }, {
            path: 'edit-borrow',
            name: 'EditBorrow',
            component: () =>
                import ('../views/borrow/EditBorrow.vue')
        }, {
            path: 'retur',
            name: 'retur',
            component: () =>
                import ('../views/retur/Retur.vue')
        }, ]
    },
    {
        path: '*',
        component: () =>
            import ('../views/404.vue')
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next();
    }
    const admin = sessionStorage.getItem('admin');
    if (!admin && to.path !== '/login') {
        return next("/login");
    }

    next();
})

export default router